﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using NHibernate.Expression;
using SimplePMT.Components;

namespace SimplePMT.Control
{
    public partial class NotificationControl : System.Web.UI.UserControl
    {
        public int taskid = 0;
        public int attachmentid = 0;
        public string notificationType = string.Empty;
        public string notificationDefaultSubject = string.Empty;
        public string notificationDefaultBody = string.Empty;
        public string notificationMemberList = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            notificationMemberList = BuildMemberList();
            if (Request.QueryString["attachmentid"] != null)
            {
                notificationType = "Attachment Notification";
                attachmentid = Convert.ToInt32(Request.QueryString["attachmentid"]);
                Data.Attachment attachment = Data.Attachment.Find(attachmentid);
                notificationDefaultSubject = attachment.Title;
            }

            if (Request.QueryString["taskid"] != null)
            {
                using (new SessionScope())
                {
                    
                    notificationType = "Task Notification";
                    taskid = Convert.ToInt32(Request.QueryString["taskid"]);
                    Task t = Task.Find(taskid);
                    notificationDefaultBody = t.Notes[t.Notes.Count - 1].Notes;
                    TaskContainerItem[] tci = TaskContainerItem.FindAllByProperty("TaskID", taskid);
                    if (tci[0].TaskContainer.ParentContainerID > 0)
                    {
                        notificationDefaultSubject = GetRecursiveTaskTreePath(TaskContainer.Find(tci[0].TaskContainer.ParentContainerID)) + " : " + tci[0].TaskContainer.Label + " : " + t.Title; ;
                    }
                    else
                    {
                        notificationDefaultSubject = tci[0].TaskContainer.Label + " : " + t.Title;
                    }
                }
            }

            using (new SessionScope())
            {
                
            }

            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        private string BuildMemberList()
        {
            StringBuilder memberList = new StringBuilder();
            memberList.Append("<table width=100%>");
            IList<Member> members = Member.FindAll();
            string[] idList = new string[memberList.Length];
            int row = 1;
            int i = 0;
            foreach (Member member in members)
            {
                idList[i] = member.ID.ToString();
                if (row == 1)
                {
                    memberList.Append("<tr>\n");
                }

                memberList.Append("<td align=right>" + FormElement.CheckBox("chk" + member.ID, member.ID.ToString(), member.FirstName, "", true, false) + "</td>\n<td>" + member.FirstName + " " + member.LastName + "</td>\n");

                if (row == 3) {
                    memberList.Append("</tr>\n");
                    row = 0;
                }
                row++;
                i++;
            }
            memberList.Append("</table>");
            memberList.Append("<script>\n");
            memberList.Append("var idList = Array();");

            i = 0;
            foreach (string id in idList)
            {
                memberList.Append("idList[" + i + "] = document.notificationForm.chk" + id + ";");
                i++;
            }
            memberList.Append("</script>\n");
            return memberList.ToString();
        }

        public string GetRecursiveTaskTreePath(TaskContainer container)
        {
            string tmpPath = container.Label;

            if (container.ParentContainerID > 0)
            {
                tmpPath = GetRecursiveTaskTreePath(TaskContainer.Find(container.ParentContainerID)) + " : " + tmpPath;
            }

            return tmpPath;
        }
    }
}